//------------------------------------------------------------------------------ 
// <copyright file="JAnimation.cs" company="Fm Computer">
//     Copyright (c) Fm Computer di Menatti Fulvia.  All rights reserved.
// </copyright>
//----------------------------------------------------------------------------- 
namespace FmjQuery
{
    using System.ComponentModel;
    using System.Text;
    using System.Web.UI;

    /// <summary>
    /// Create jquery animation
    /// </summary>
    [ToolboxData("<{0}:JAnimation runat=server><{0}:JAnimation>")]
    [DefaultProperty("TargetControlID")]
    public class JAnimation : JQueryExtendControl
    {
        #region Field
        /// <summary>
        /// Collection of animation
        /// </summary>
        private Animations tanimation;
        #endregion

        #region Costruct
        /// <summary>
        /// Initializes a new instance of the JAnimation class.
        /// </summary>
        public JAnimation()
        {
            this.IsRenderControl = false;
        }
        #endregion

        #region Property
        /// <summary>
        /// Gets collection of animation
        /// </summary>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public Animations Animations 
        {
            get
            {
                if (this.tanimation == null)
                {
                    this.tanimation = new Animations();
                }

                return this.tanimation;
            }
        }
        #endregion

        #region Overrides of JQueryExtendControl
        /// <summary>
        /// Return list of webResource
        /// </summary>
        /// <returns>web resources</returns>
        public override JWebSources GetResources()
        {
            return null;
        }

        /// <summary>
        /// Gets list of css image resources
        /// </summary>
        /// <returns>list of images</returns>
        public override CssResources GetCssResources()
        {
            return null;
        }

        /// <summary>
        /// Gets list of client nameSpace
        /// </summary>
        /// <returns>list of namespace</returns>
        public override ClientNameSpaces GetClientNameSpace()
        {
            return null;
        }

        /// <summary>
        /// Create jQuery control
        /// </summary>
        public override void CreateJControl()
        {
            if (this.Enabled)
            {
                if (this.Animations.Count > 0)
                {
                    // create script
                    StringBuilder builder = new StringBuilder();

                    foreach (Animation animation in this.Animations)
                    {
                        string activeID;

                        if (!string.IsNullOrEmpty(animation.ActiveAnimationIDControl))
                        {
                            activeID = animation.IsClientActiveControl ? animation.ActiveAnimationIDControl : FindControl(animation.ActiveAnimationIDControl).ClientID;
                        }
                        else
                        {
                            activeID = this.TargetControlClientID;
                        }

                        builder.AppendFormat("$('#{0}').bind('{1}', ", activeID, animation.Event);

                        // create child animate script
                        animation.Steps.Sort((f, s) => f.Index.CompareTo(s.Index));

                        int close = 0;
                        for (int x = 0; x < animation.Steps.Count; x++)
                        {
                            AnimationStep step = animation.Steps[x];

                            builder.Append("function(){");
                            builder.AppendFormat("$('#{0}').animate(", this.TargetControlClientID);
                            builder.Append("{");

                            // animate stype
                            bool first = true;
                            foreach (StyleAction action in step.Actions)
                            {
                                if (!first)
                                {
                                    builder.Append(",");
                                }
                                else
                                {
                                    first = false;
                                }

                                builder.AppendFormat("{0}: '{1}'", action.Attribute, action.Value);
                            }

                            builder.Append("}");

                            // duration and callback
                            bool speedExplicit = false;
                            if (step.Speed != SpeedType.normal || step.Duration > 0)
                            {
                                if (step.Speed != SpeedType.normal)
                                {
                                    builder.AppendFormat(", '{0}'", step.Speed);
                                }
                                else
                                {
                                    builder.AppendFormat(", {0}", step.Duration);
                                }

                                speedExplicit = true;
                            }

                            if (x < (animation.Steps.Count - 1))
                            {
                                if (!speedExplicit)
                                {
                                    builder.AppendFormat(", '{0}', null, ", step.Speed);
                                }
                                else
                                {
                                    builder.Append(", null, ");
                                }

                                close += 1;
                            }
                        }

                        // close sub function
                        for (int x = 0; x <= close; x++)
                        {
                            builder.Append(");}");
                        }

                        builder.Append(");");
                    }

                    Utility.RegisterStartUpScript(this.Page, string.Concat("Animation_", this.ClientID), builder.ToString());
                }
            }
        }
        #endregion
    }
}